package com.swufe.week6;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RateTask implements Runnable {
  private  static final String TAG="RateTask";
  private Handler handler;
    @Override
    public void run() {
        Log.i(TAG, "run: .........");
        List<String> ret =new ArrayList<String>();
        try {
           // Thread.sleep(5000);

            String url = "http://www.usd-cny.com/bankofchina.htm";
            Document doc = Jsoup.connect(url).get();
            Log.i(TAG, "run: " + doc.title());
            Element tables = doc.getElementsByTag("table").first();
            Elements trs=tables.getElementsByTag("tr");
            for(Element tr:trs){
                Elements tds=tr.getElementsByTag("td");
                if(tds.size()>0){
                    String str =tds.first().text();
                    String val=tds.get(5).text();
                    HashMap<String,String>map=new HashMap<String,String>();
                    map.put("itemTitle",str);
                    map.put("itemDetail",val);
                   // ret.add(map);
                    Log.i(TAG, "run: td="+str);
                    Log.i(TAG, "run: rate="+val);
                }
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //返回给主线程
     //   Message msg = handler.obtainMessage(9,retlist);
     //   msg.obj=retlist;
      //  handler.sendMessage(msg);
    }
}

